package com.japhet.stripesdemo.business.user;

import com.japhet.stripesdemo.business.stripe.StripeService;
import com.japhet.stripesdemo.business.user.bo.UserAddPaymentMethodBO;
import com.japhet.stripesdemo.business.user.bo.UserLoginBO;
import com.japhet.stripesdemo.business.user.bo.UserRegistryBO;
import com.japhet.stripesdemo.common.ResponseR;
import com.japhet.stripesdemo.dao.entity.User;
import com.japhet.stripesdemo.dao.entity.UserPaymentMethod;
import com.japhet.stripesdemo.dao.service.UserPaymentMethodService;
import com.japhet.stripesdemo.dao.service.UserService;
import com.stripe.exception.StripeException;
import com.stripe.model.Customer;
import com.stripe.model.PaymentMethod;
import com.stripe.param.PaymentMethodAttachParams;
import com.xiaoleilu.hutool.json.JSONUtil;
import com.xiaoleilu.hutool.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

@Slf4j
@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private StripeService stripeService;

    @Autowired
    private UserPaymentMethodService userPaymentMethodService;

    @PostMapping("login")
    public String login(@RequestBody UserLoginBO userLoginBO) {
        User user = userService.lambdaQuery().eq(User::getName, userLoginBO.getName()).one();
        if (Objects.isNull(user)) {
            ResponseR r = ResponseR.error();
            return JSONUtil.toJsonStr(r);
        }

        ResponseR<User> r = ResponseR.ok().data(user);
        return JSONUtil.toJsonStr(r);
    }

    @PostMapping("registry")
    public String registry(@RequestBody UserRegistryBO userRegistryBO) throws StripeException {
        User user = userService.lambdaQuery().eq(User::getName, userRegistryBO.getName()).one();
        if (Objects.isNull(user)) {
            user = new User();
            user.setName(userRegistryBO.getName());
            user.setEmail(userRegistryBO.getEmail());
            Customer customer = stripeService.create(userRegistryBO.getName(), userRegistryBO.getEmail());
            user.setStripeCustomerId(customer.getId());
            userService.save(user);
        }
        ResponseR r = ResponseR.ok().data(user);
        return JSONUtil.toJsonStr(r);
    }

    @PostMapping("addPaymentMethod")
    public String addPaymentMethod(@RequestBody UserAddPaymentMethodBO userAddPaymentMethodBO) throws StripeException {
        UserPaymentMethod userPaymentMethod = userPaymentMethodService.lambdaQuery()
                .eq(UserPaymentMethod::getPaymentMethodId, userAddPaymentMethodBO.getPaymentMethodId())
                .one();
        if (Objects.nonNull(userPaymentMethod)) {
            ResponseR r = ResponseR.error().message(StrUtil.format("已经存在当前支付方式id{}", userAddPaymentMethodBO.getPaymentMethodId()));
            return JSONUtil.toJsonStr(r);
        }
        userPaymentMethod = new UserPaymentMethod();
        userPaymentMethod.setUserId(userAddPaymentMethodBO.getUserId());
        userPaymentMethod.setPaymentMethodId(userAddPaymentMethodBO.getPaymentMethodId());
        userPaymentMethodService.save(userPaymentMethod);

        User user = userService.getById(userAddPaymentMethodBO.getUserId());

        //创建一个 PaymentMethodAttachParams 对象
        PaymentMethodAttachParams params = PaymentMethodAttachParams.builder()
                .setCustomer(user.getStripeCustomerId())
                .build();

        // 使用 paymentMethodId 和 params 进行关联
        PaymentMethod paymentMethodRes = PaymentMethod.retrieve(userAddPaymentMethodBO.getPaymentMethodId());
        paymentMethodRes.attach(params);

        System.out.println("PaymentMethod successfully attached to Customer.");
        return JSONUtil.toJsonStr(ResponseR.ok().data(paymentMethodRes));
    }
}
